#include "LCAUIndex.h"
#include "LRESScreen.h"
#include "../std.h"
#include "../Widgets/Colours.h"
#include "../Widgets/UIBuilder.h"
#include "../Utilities/Convert.h"
#include "../Utilities/trim.h"


LCAUIndex::LCAUIndex(){

	mProcessor = new LCAUProcessor();
	mLayout = createMainLayout(getStringNotIterative(MAIN_TEXT_START + 2), getStringNotIterative(L_UPDATE), " " , getStringNotIterative(L_BACK));

	mItems = (ListBox*)mLayout->getChildren()[1];

	mSoftkey = (Widget*)mLayout->getChildren()[2];
	this->setMain(mLayout);

	mGetData = false;
}

LCAUIndex::~LCAUIndex(){
	delete mLayout;
}

void LCAUIndex::keyPressEvent(int keyCode, int nativeCode)
{
	switch(keyCode)
	{
		case MAK_UP:
		case MAK_2:
			mItems->selectPreviousItem();
			break;
		case MAK_DOWN:
		case MAK_8:
			mItems->selectNextItem();
			break;
		case MAK_SOFTRIGHT:
		case MAK_6:
			APPCONTROLLER->back();
			break;
		case MAK_FIRE:
			LRESScreen* screen= (LRESScreen*)APPCONTROLLER->getScreen("LRESScreen");
			screen->indexAction = 1;
			LRESItemWidget* widget = (LRESItemWidget*)mItems->getChildren()[mItems->getSelectedIndex()];

			LRESItem* newitem = new LRESItem();
			newitem->companyid = widget->getId();
			newitem->companyname = widget->getCompanyName();
			newitem->drawdate = widget->getDrawDate();
			newitem->istoday = widget->getIsToday();
			newitem->outofdate = widget->getIsOod();
			newitem->result = widget->getResult();
			newitem->shortcode = widget->getShortCode();
			newitem->smscnt = widget->getSmsCnt();

			screen->setLRESItem(newitem);
			delete newitem;

			APPCONTROLLER->show("LRESScreen");
			break;
		case MAK_SOFTLEFT:
			if (mItems->getChildren()[0] != NULL){
				mItems->setSelectedIndex(0);
			}
			mItems->clear();
			Label* l = (Label*)mSoftkey->getChildren()[1];
			l->setCaption("Loading");
			l->update();
			mProcessor->addRequest(APPCONTROLLER->getUrl(), this);
			break;
	}
}

void LCAUIndex::itemReceived(LCAUItem& item){

	LRESItemWidget* newItem = new LRESItemWidget(0, 0, APPCONTROLLER->getScreenWidth(), HEIGHTITEM, mItems);

	newItem->setCompanyName(item.companyname);
	newItem->setCompanyFont(APPCONTROLLER->getFont("default"));
	newItem->setDrawDate(item.drawdate);
	newItem->setDrawDateFont(APPCONTROLLER->getFont("default"));

	newItem->setId(item.companyid);
	newItem->setSmsCnt(item.smscnt);
	newItem->setShortCode(item.shortcode);
	newItem->setResult(item.result);
	newItem->setIsToday(item.ischarging);
	newItem->setIsOod(item.outofdate);

	newItem->setSkin(APPCONTROLLER->getSkin("default"));
	//newItem->addWidgetListener(this);

	mItems->setSelectedIndex(0);
	mItems->getChildren()[0]->setSelected(true);
}

void LCAUIndex::triggered(Widget* sender)
{
	//Act as if fire has been pressed
	//keyPressEvent(MAK_FIRE, 0);
}

void LCAUIndex::pointerReleaseEvent(MAPoint2d point){

}

void LCAUIndex::pointerPressEvent(MAPoint2d point){
	Point p;
	p.set(point.x, point.y);

	if (mItems->contains(p)){
		for(int i=0; i< mItems->getChildren().size(); i++){
			if (mItems->getChildren()[i]->contains(p)){
				int index = mItems->getSelectedIndex();
				if (index == i) keyPressEvent(MAK_FIRE, 0);
				else mItems->setSelectedIndex(i);
				break;
			}
		}
	}
	else if (mSoftkey->contains(p)){
		if(mSoftkey->getChildren()[0]->contains(p)) keyPressEvent(MAK_SOFTLEFT, 0);
		else if (mSoftkey->getChildren()[2]->contains(p)) keyPressEvent(MAK_SOFTRIGHT, 0);
	}
}

void LCAUIndex::processingComplete(){
	lprintfln("process LCAU complete");
	mGetData = true;
	Label* l = (Label*)mSoftkey->getChildren()[1];
	l->setCaption(" ");
	l->update();
}

void LCAUIndex::show(){
	if (!mGetData){
		Label* l = (Label*)mSoftkey->getChildren()[1];
		l->setCaption("Loading");
		l->update();
		mProcessor->addRequest(APPCONTROLLER->getUrl(), this);
	}
	Screen::show();
}
